package com.whl.pet.controller;

import com.whl.basic.exception.BusinessException;
import com.whl.pet.dto.MsgDto;
import com.whl.pet.service.ISearchMasterMsgService;
import com.whl.pet.domain.SearchMasterMsg;
import com.whl.pet.query.SearchMasterMsgQuery;
import com.whl.basic.util.AjaxResult;
import com.whl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null){
                    searchMasterMsgService.update(searchMasterMsg);
            }else{
                    searchMasterMsgService.add(searchMasterMsg);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 发布寻主消息
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg, request);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败");
        }
    }

    /**
     * 接口：用户寻主列表: 用于登录之后在个人中心查询自己所有状态的寻主消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.user(query, request);
    }

    /**
     * 接口：待审核或驳回：查询待审核或驳回的寻主消息，平台管理员才能查询
     * 可以通过按钮权限限制
     * @param query
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0); // 0 - 说明审核待处理
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 接口：审核已完成：查询已完成的寻主消息，平台管理员才能查询
     * 可以通过按钮权限限制
     * @param query
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        query.setState(2); // 2- 说明处理完成
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 接口：寻主池：查询审核通过还没有分配店铺或者被拒单的寻主消息
     * state = 1，shopId为null
     * @param query
     * @return
     */
    @PostMapping("/searchMasterPool")
    public PageList<SearchMasterMsg> searchMasterPool(@RequestBody SearchMasterMsgQuery query) {
        query.setState(1); // 1 - 说明审核通过待处理
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 接口：审核成功待处理：查询审核通过并已经分配了店铺的寻主消息
     * state = 1，shopId不为null
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> tohandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        return searchMasterMsgService.tohandle(query, request);
    }

    /**
     * 拒单
     * @param msgId
     * @return
     */
    @GetMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"拒单失败");
        }
    }

    /**
     * 接单
     * @param msgDto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody MsgDto msgDto){
        try {
            searchMasterMsgService.accept(msgDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"接单失败");
        }
    }
}
